/**
 * Created by Administrator on 2016/4/15.
 */
var code ; //在全局 定义验证码

function createCode()
{

    code = "";
    var codeLength = 4;//验证码的长度
    var checkCode = document.getElementById("checkCode");
    var selectChar = new Array(0,1,2,3,4,5,6,7,8,9);//所有候选组成验证码的字符，当然也可以用中文的

    for(var i=0;i<codeLength;i++)
    {

        var charIndex = Math.floor(Math.random()*10);
        code +=selectChar[charIndex];

    }

    if(checkCode)
    {
        checkCode.className="l_code";
        checkCode.value = code;
    }

}

$(function(){

    $(".have").focus(function(){
        //alert(1);
        //console.log(1);
        $("#footer").hide();
    });
    $(".have").blur(function(){
        $("#footer").show();
    });
    createCode()
    $("#txt_username").focus(function(){
      /*  $(this).css("color","black");*/
        var email_txt = $(this).val();
        if (email_txt == this.defaultValue) {
            $(this).val("");
        }
    })
    $("#txt_username").blur(function(){

        var email_txt = $(this).val();
        if (email_txt == "") {
          /*  $(this).css("color","#ccc");*/
            $(this).val(this.defaultValue);
        }
    })

    $("#txt_password1").focus(function(){
        $("#footer").hide();
        var email_txt = $(this).val();
        if (email_txt == this.defaultValue) {
            $(this).val("");
            $("#txt_password").show();
            $("#txt_password").val("");
            $("#txt_password1").hide();
            $("#txt_password").focus();


        }
    })
    $("#txt_password").focus(function(){
        $("#footer").hide();
    })
    $("#txt_password1").blur(function(){
        $("#footer").hide();
    })
    $("#txt_password").blur(function(){
        $("#footer").show();
        var email_txt = $(this).val();
        if (email_txt == "") {
            $(this).val(this.defaultValue);
            $("#txt_password1").show();
            $("#txt_password1").val(this.defaultValue);
            $("#txt_password").hide();
        }
    })

    $("#yzm").focus(function(){
        var email_txt = $(this).val();
        if (email_txt == this.defaultValue) {
            $(this).val("");
        }
    })
    $("#yzm").blur(function(){
        var email_txt = $(this).val();
        if (email_txt == "") {
            $(this).val(this.defaultValue);
        }
    })

    $("#yanzhengma").focus(function(){
        /*  $(this).css("color","black");*/
        var email_txt = $(this).val();
        if (email_txt == this.defaultValue) {
            $(this).val("");
        }
    })
    $("#yanzhengma").blur(function(){

        var email_txt = $(this).val();
        if (email_txt == "") {
            /*  $(this).css("color","#ccc");*/
            $(this).val(this.defaultValue);
        }
    })
    $("#pwd").focus(function(){
        /*  $(this).css("color","black");*/
        var email_txt = $(this).val();
        if (email_txt == this.defaultValue) {
            $(this).val("");
        }
    })
    $("#pwd").blur(function(){

        var email_txt = $(this).val();
        if (email_txt == "") {
            /*  $(this).css("color","#ccc");*/
            $(this).val(this.defaultValue);
        }
    })
    $("#login").click(function(){
        var user=$("#txt_username").val();
        var pwd=$("#txt_password").val();
        var yzm=$("#yzm").val();
        if(user.length<1){
            alert("账号不能为空");
            return false;
        }else if(pwd.length<1){
            alert("密码不能为空");
            return false;
        }
    }).mousedown(function(){
        $("#login").addClass("l_anniu")
    }).mouseup(function(){
        $("#login").removeClass("l_anniu")
    });
})

$(document).ready(function () {
    if ($.cookie("rmbUser") == "true") {
        $("#ck_rmbUser").attr("checked", true);

        /*   $("#txt_username").val($.cookie("username"));*/
        $("#txt_password").val($.cookie("password"));
        $("#txt_password1").hide();
        $("#txt_password").show()
    }
    if ($.cookie("rmbUser1") == "true") {
        $("#ck_rmbUser1").attr("checked", true);
        $("#txt_username").val($.cookie("username"));

    }
});

function Save() {
    var username = $('#txt_username').val();
    var password = $('#txt_password').val();
    var codes = $('#yzm').val();
    if(codes.length<1){
        alert("验证码不能为空");
        return false;
    } else if(codes != code) {
        alert("验证码错误");

    }else{
        $.ajax({
            url: "../hxclub/authen/login.do",
            async: false,
            type : "post",
            dataType : "json",
            data:{"code": "" + username + "", "pwd": "" + password + ""/*, "codes": "" + codes + ""*/},

            success:function(d){
                if(!d.success){
                    alert(d.statusCode)
                }else{
                  /*   window.location.href="../index.html";*/
                    console.log('login.html --> d.data = '+d.data);
                    var fromUrl = getSearchParam('redirectUrl');//是从fromUrl来到登录界面的，所以fromUrl应该设置
                    if(fromUrl != null && fromUrl != ''){
                        window.location.href = fromUrl;
                    } else {//如果找不到上一张页面，则做浏览器退页操作
                        window.location.href = 'index.html';
                        /* history.go(-1);
                         location.reload(); */
                    }
                   /*  window.location.href="../index.html";*/
                }
            }
        });
    }
    /*      else if (codes != "") {
     if (username != "" && password != "") {
     }
     }*/

    //记住密码
    if ($("#ck_rmbUser").attr("checked")) {
        var str_username = $("#txt_username").val();
        var str_password = $("#txt_password").val();
        $.cookie("rmbUser", "true", { expires: 7 }); //存储一个带7天期限的cookie
        /*    $.cookie("username", str_username, { expires: 7 });*/
        $.cookie("password", str_password, { expires: 7 });
    }
    else {
        $.cookie("rmbUser", "false", { expire: -1 });
        /*       $.cookie("username", "", { expires: -1 });*/
        $.cookie("password", "", { expires: -1 });
    }
    //记住用户名
    if ($("#ck_rmbUser1").attr("checked")) {
        var str_username = $("#txt_username").val();

        $.cookie("rmbUser1", "true", { expires: 7 }); //存储一个带7天期限的cookie
        $.cookie("username", str_username, { expires: 7 });
    }
    else {
        $.cookie("rmbUser1", "false", { expire: -1 });
        $.cookie("username", "", { expires: -1 });
        $.cookie("password", "", { expires: -1 });
    }

};
